package com.szqhyxj.youxiangju.biz.realNameAuth.idcard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.PhotoBridge;
import com.szqhyxj.youxiangju.biz.realNameAuth.bankcard.BankCardAuthActivity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.picker.DatePicker;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.PickerUtil;
import com.szqhyxj.youxiangju.sys.components.view.popview.SingleDialog;
import com.szqhyxj.youxiangju.sys.util.FileUtils;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * Created by leung on 2018/10/30.
 */

public class IDCardAuthActivity extends BaseActivity implements IDCardAuthContract.View, NavigationBar.NavigationBarListener, CustomAdapt {

    private static final int REQUEST_TAKE_PHOTO_FACE = 1;
    private static final int REQUEST_TAKE_PHOTO_BACK = 2;
    private static final int REQUEST_GALLERY_FACE = 3;
    private static final int REQUEST_GALLERY_BACK = 4;
    Boolean faceFlag = false;
    Boolean backFlag = false;
    private static final String TAG = IDCardAuthActivity.class.getSimpleName();
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.idcard_face)
    ImageView idcardFace;
    @BindView(R.id.idcard_face_take)
    Button idcardFaceTake;
    @BindView(R.id.idcard_back)
    ImageView idcardBack;
    @BindView(R.id.idcard_back_take)
    Button idcardBackTake;
    @BindView(R.id.icard_info)
    LinearLayout icardInfo;
    @BindView(R.id.next)
    Button next;
    @BindView(R.id.ocrtip)
    TextView ocrtip;
    @BindView(R.id.tiparea)
    LinearLayout tiparea;

    @BindView(R.id.realname)
    EditText name;
    @BindView(R.id.realidcardno)
    EditText idcardno;
    @BindView(R.id.startvalidity)
    TextView startvalidity;
    @BindView(R.id.endvalidity)
    TextView endvalidity;


    private IDCardAuthContract.Presenter presenter;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, IDCardAuthActivity.class);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_name_auth);
        ButterKnife.bind(this);
        presenter = new IDCardAuthPresenter(this);
        // presenter.request();
        navigationBar.setNavigationBarListener(this);
        name.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setName(s + "");
            }
        });
        idcardno.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setIdCard(s + "");
            }
        });

    }

    @Override
    public boolean isBaseOnWidth() {
        return true;
    }

    @Override
    public float getSizeInDp() {
        return 360;
    }


    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_CANCELED) {
            return;
        }
        // 拍照或者选择照片成功的时候，交给 presenter 处理
        if (requestCode == REQUEST_TAKE_PHOTO_FACE) {
            try {
                presenter.idcardFaceFromTakePhoto(this);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        if (requestCode == REQUEST_TAKE_PHOTO_BACK) {
            try {
                presenter.idcardBackFromTakePhoto(this);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (requestCode == REQUEST_GALLERY_FACE) {
            File tempFile = FileUtils.uri2File(this, data.getData());
            presenter.idcardFaceFromGallery(tempFile);
        }
        if (requestCode == REQUEST_GALLERY_BACK) {
            File tempFile = FileUtils.uri2File(this, data.getData());
            presenter.idcardBackFromGallery(tempFile);
        }

        navigationBar.setNavigationBarListener(this);
    }

    @Override
    public void previewFace(File file) {
        LoadImageUtils.loadImage(idcardFace, file);
    }

    @Override
    public void showFace() {
        idcardFace.setImageDrawable(getResources().getDrawable(R.mipmap.zhengmian));
    }

    @Override
    public void previewBack(File file) {
        LoadImageUtils.loadImage(idcardBack, file);
    }

    @Override
    public void showBack() {
        idcardBack.setImageDrawable(getResources().getDrawable(R.mipmap.fanmian));
    }

    @Override
    public void showIDCardFace(IDCardFaceInfo idCardFaceInfo) {

    }

    @Override
    public void showIDCardBack(IDCardBackInfo idCardBackInfo) {

    }

    @Override
    public void showOCRName(String s) {
        name.setText(s);
    }

    @Override
    public void showOCRIdno(String no) {
        idcardno.setText(no);
    }

    @Override
    public void showStartDate(String s) {
        startvalidity.setText(s);
    }

    @Override
    public void showEndDate(String s) {
        endvalidity.setText(s);
    }


    @Override
    public void showOCRValidity(String date) {
        //   uservalidity.setText(date);
    }

    @Override
    public void showOCRTip(String s) {
        ocrtip.setVisibility(View.VISIBLE);
        ocrtip.setText(s);
    }

    @Override
    public void enshowOCRTip() {
        ocrtip.setVisibility(View.GONE);
    }

    @Override
    public void showInfo() {
        tiparea.setVisibility(View.GONE);
        icardInfo.setVisibility(View.VISIBLE);
    }

    @Override
    public void enshowInfo() {
        icardInfo.setVisibility(View.GONE);
        tiparea.setVisibility(View.VISIBLE);
    }

    @Override
    public void commitSuccess(String s) {
        BankCardAuthActivity.start(this, name.getText().toString());
    }

    @Override
    public void commitFial() {
        Toast.makeText(this, "上传失败", Toast.LENGTH_LONG).show();
    }

    @Override
    public void navToTakePhotoFace(File photoFile) {
        PhotoBridge.startToTakePicture(this, REQUEST_TAKE_PHOTO_FACE, photoFile);
    }

    @Override
    public void navToTakePhotoBack(File photoFile) {
        PhotoBridge.startToTakePicture(this, REQUEST_TAKE_PHOTO_BACK, photoFile);
    }

    @Override
    public void navToGalleryForFace() {
        PhotoBridge.startToGallery(this, REQUEST_GALLERY_FACE);
    }

    @Override
    public void navToGalleryForBack() {
        PhotoBridge.startToGallery(this, REQUEST_GALLERY_BACK);
    }


    @Override
    public void uploadSuccessFace() {
        faceFlag = true;
    }

    @Override
    public void uploadSuccessBack() {
        backFlag = true;

    }

    @Override
    public void showFaceUrl(String s) {
        Glide.with(this).load(s).into(idcardFace);

    }

    @Override
    public void showBackUrl(String s) {
        Glide.with(this).load(s).into(idcardBack);
    }

    @Override
    public void flagUrl() {
        faceFlag = true;
        backFlag = true;
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showCommit() {
        next.setBackgroundResource(R.drawable.background_btn04);
        next.setEnabled(true);
    }

    @Override
    public void showEncommit() {
        next.setBackgroundResource(R.drawable.background_btn_grey04);
        next.setEnabled(false);
    }

    @Override
    public void showOcrFail(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showIDCardFaceUploadSuccess(String fileUrl) {
        Log.i(TAG, "showIDCardFaceUploadSuccess: " + fileUrl);
    }

    @Override
    public void showIDCardFaceUploadFail(String message) {
        idcardFace.setImageDrawable(getResources().getDrawable(R.mipmap.zhengmian));
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        Log.i(TAG, "showIDCardFaceUploadFail: " + message);
    }

    @Override
    public void showIDCardBackUploadSuccess(String fileUrl) {
        Log.i(TAG, "showIDCardBackUploadSuccess: " + fileUrl);
    }

    @Override
    public void showIDCardBackUploadFail(String message) {
        idcardBack.setImageDrawable(getResources().getDrawable(R.mipmap.fanmian));
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        Log.i(TAG, "showIDCardBackUploadFail: " + message);
    }


    @OnClick({R.id.idcard_face_take, R.id.idcard_back_take, R.id.next, R.id.startvalidity, R.id.endvalidity})
    public void onClick(View view) {
        final SingleDialog singleDialog = new SingleDialog(this);
        switch (view.getId()) {
            case R.id.idcard_face_take:


                singleDialog.setISharePlatformListener(new SingleDialog.ISharePlatformListener() {
                    @Override
                    public void sharePlatform(String platform) {

                    }

                    @Override
                    public void onClickAlbum(View view) {
                        presenter.selectFromGalleryForFace();
                        singleDialog.dismiss();
                    }

                    @Override
                    public void onTakePhoto(View view) {
                        try {
                            presenter.takePhotoForFace(IDCardAuthActivity.this);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        singleDialog.dismiss();
                    }
                });
                singleDialog.show();

                break;
            case R.id.idcard_back_take:

                singleDialog.setISharePlatformListener(new SingleDialog.ISharePlatformListener() {
                    @Override
                    public void sharePlatform(String platform) {

                    }

                    @Override
                    public void onClickAlbum(View view) {
                        presenter.selectFromGalleryForBack();
                        singleDialog.dismiss();
                    }

                    @Override
                    public void onTakePhoto(View view) {
                        try {
                            presenter.takePhotoForBack(IDCardAuthActivity.this);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        singleDialog.dismiss();
                    }
                });
                singleDialog.show();


                break;
            case R.id.next:
                if (faceFlag == true && backFlag == true) {
                    presenter.commint();
                } else {
                    presenter.setEncommit();
                    Toast.makeText(this, "请填写完整信息！", Toast.LENGTH_LONG).show();
                }

                break;
            case R.id.startvalidity:
                PickerUtil.onMonthDayPicker(this, new DatePicker.OnYearMonthDayPickListener() {
                    @Override
                    public void onDatePicked(String year, String month, String day) {

                        if (year.equals("长期")) {
                            startvalidity.setText("请选择开始时间");

                            Toast.makeText(IDCardAuthActivity.this, "起始日期不可以是长期", Toast.LENGTH_LONG).show();
                        } else {
                            startvalidity.setText(year + "/" + month + "/" + day);
                            presenter.setStartDate(year + "-" + month + "-" + day);
                        }

                    }
                });
                break;
            case R.id.endvalidity:
                PickerUtil.onMonthDayPicker(this, new DatePicker.OnYearMonthDayPickListener() {
                    @Override
                    public void onDatePicked(String year, String month, String day) {
                        endvalidity.setText(year + "/" + month + "/" + day);
                        if (year.equals("长期")) {
                            presenter.setEndDate("2099" + "-" + "12" + "-" + "31");
                        } else {
                            presenter.setEndDate(year + "-" + month + "-" + day);

                        }
                    }
                });
                break;
        }
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }
}